package com.qph.utils.encrypt;

import javax.crypto.Cipher;
import net.iharder.Base64;

public class DBEncrypt {

	public String eCode(String needEncrypt){
		try {
			Cipher enCipher = Cipher.getInstance("DES");
			javax.crypto.SecretKey key = Key.loadKey();
			enCipher.init(1, key);
			byte result[] = enCipher.doFinal(needEncrypt.getBytes());
			
			return Base64.encodeBytes(result);
		} catch (Exception e) {
			throw new IllegalStateException("System doesn't support DES algorithm.");
		}
	}

	public String dCode(byte result[]){
		try {
			Cipher deCipher = Cipher.getInstance("DES");
			deCipher.init(2, Key.loadKey());
			byte strByte[] = deCipher.doFinal(Base64.decode(result));
			return new String(strByte);
		} catch (Exception e) {
			throw new IllegalStateException("System doesn't support DES algorithm.");
		}
	}

	public static void main(String[] args){
    	//String s = "zfxy";
		//jdbc:mysql:// localhost:3306/myproject
		String s = "123456";
    	DBEncrypt p = new DBEncrypt();
    	String afterE = p.eCode(s);
    	System.out.println(afterE);
    	System.out.println(p.dCode("urryk08cSuU=".getBytes()));
    }
}